package com.emep.changzhi.analyse.service.systeminfo;

import com.emep.changzhi.analyse.entity.systeminfo.SysMenuInfo;
import com.emep.changzhi.analyse.repository.systeminfo.SysMenuInfoRepository;
import com.emep.changzhi.analyse.utils.MapUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;
import java.util.Map;

/**
 * @author Wangcc
 * @version V1.0
 * @Description: 菜单信息表业务器
 * @date 2018/2/26 10:58
 */
@Slf4j
@Service
public class SysMenuInfoService {
    
    @Resource
    private SysMenuInfoRepository sysMenuInfoRepository;
    
    /**
     *
     * 不含查询条件的分页获取数据
     *
     * @param pageNo
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Page<SysMenuInfo> findAll(int pageNo, int pageSize){
        try {
            return sysMenuInfoRepository.findAll(new PageRequest(pageNo-1, pageSize, new Sort(Sort.Direction.ASC, "id")));
        } catch (Exception e) {
            log.error(e.getClass().getName()+":"+ e.getMessage());
            log.error(e.getLocalizedMessage());
            return null;
        }
    }

    /**
     *
     * 新增菜单信息
     *
     * @param payload
     * @return
     * @throws Exception
     */
    public SysMenuInfo add(Map<String, Object> payload) {
        SysMenuInfo SysMenuInfo = null;
        try {
            SysMenuInfo SysMenuInfo1 = (SysMenuInfo) MapUtils.convertMap(SysMenuInfo.class,payload);
            SysMenuInfo = sysMenuInfoRepository.save(SysMenuInfo1);
        }catch (Exception e){
            log.error(e.getClass().getName()+":"+ e.getMessage());
            log.error(e.getLocalizedMessage());
        }
        return SysMenuInfo;
    }

    /**
     *
     * 根据id查询菜单信息
     *
     * @param id
     * @return
     */
    public SysMenuInfo findId(String id){
        SysMenuInfo sysUserInfo = null;
        try {
            sysUserInfo = sysMenuInfoRepository.findSysRoleInfoById(id);
        }catch (Exception e){
            log.error(e.getClass().getName()+":"+ e.getMessage());
            log.error(e.getLocalizedMessage());
        }
        return sysUserInfo;
    }

    /**
     *
     * 根据id 删除菜单信息
     *
     * @param id
     */
    public Boolean delete(String id) {
        Boolean result = false;
        try {
            Integer result1 = sysMenuInfoRepository.deleteById(id);
            if(result1 == 1){
                result = true;
            }
        }catch (Exception e){
            log.error(e.getClass().getName()+":"+ e.getMessage());
            log.error(e.getLocalizedMessage());
        }
        return result;
    }

    /**
     *查询所有菜单信息
     *
     * @return
     */
    public List<SysMenuInfo> findAll() {
        List<SysMenuInfo> SysMenuInfo = null;
        try {
            SysMenuInfo =  sysMenuInfoRepository.findAll();
        }catch (Exception e){
            log.error(e.getClass().getName()+":"+ e.getMessage());
            log.error(e.getLocalizedMessage());
        }
        return SysMenuInfo;
    }

}
